package com.msg.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.msg.core.constant.CommonConstants;
import com.msg.core.exception.ProjectException;
import com.msg.domain.dto.QuestionAppealDTO;
import com.msg.domain.model.SysFile;
import com.msg.domain.model.XxtMsg;
import com.msg.domain.model.XxtOfficer;
import com.msg.domain.model.XxtTown;
import com.msg.mapper.SysFileMapper;
import com.msg.mapper.XxtMsgMapper;
import com.msg.mapper.XxtOfficerMapper;
import com.msg.mapper.XxtTownMapper;
import com.msg.service.QuestionSevice;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
@RequiredArgsConstructor
public class QuestionSeviceImpl implements QuestionSevice {

    private final XxtTownMapper xxtTownMapper;
    private final XxtOfficerMapper xxtOfficerMapper;
    private final XxtMsgMapper xxtMsgMapper;
    private final SysFileMapper sysFileMapper;

    @Override
    public void appeal(QuestionAppealDTO appealDTO) {
        String townName=appealDTO.getTownName();
        String officerName=appealDTO.getOfficerName();

        XxtTown town=xxtTownMapper.selectOne(new LambdaQueryWrapper<XxtTown>()
                .eq(XxtTown::getTownName,townName)
                .or()
                .eq(XxtTown::getTownName2,townName));
        if(town==null){
            throw new ProjectException("村名不合法");
        }
        XxtOfficer officer=xxtOfficerMapper.selectOne(new LambdaQueryWrapper<XxtOfficer>()
                .eq(XxtOfficer::getOfficerName,officerName)
                .or()
                .eq(XxtOfficer::getOfficerName2,officerName)
        );
        if(officer==null){
            throw new ProjectException("干部名不合法");
        }
        XxtMsg msg=new XxtMsg();
        msg.setTownName(town.getTownName());
        msg.setTownName2(town.getTownName2());
        msg.setUserName(appealDTO.getUserName());
        msg.setPhone(appealDTO.getPhone());
        msg.setOfficerName(officer.getOfficerName());
        msg.setOfficerName2(officer.getOfficerName2());
        msg.setAppealContent(appealDTO.getAppealContent());
        msg.setCreateTime(new Date());
        msg.setDelFlag(CommonConstants.NORMAL);
        xxtMsgMapper.insert(msg);
    }

    @Override
    public List<XxtTown> townSelect() {
        List<XxtTown> towns=xxtTownMapper.selectList(new LambdaQueryWrapper<>());
        return CollUtil.isEmpty(towns)?new ArrayList<>():towns;
    }

    @Override
    public List<XxtOfficer> officerSelect() {
        List<XxtOfficer> officers=xxtOfficerMapper.selectList(new LambdaQueryWrapper<>());
        officers.forEach(officer->{
            officer.setPhone(null);
        });
        return CollUtil.isEmpty(officers)?new ArrayList<>():officers;
    }


    @Override
    public String officerQrcode(String objectType) {

        SysFile sysFile=sysFileMapper.selectOne(new LambdaQueryWrapper<SysFile>()
                .eq(SysFile::getObjectType,"officerQrcode")
                .orderByDesc(SysFile::getId)
                .last("limit 1")
        );
        if(sysFile!=null){
            return sysFile.getUrl();
        }
        return null;
    }
}
